﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace WebApplication4
{
    /// <summary>
    /// HttpAsyncHandlerTestAsync 的摘要说明
    /// </summary>
    public class HttpAsyncHandlerTestAsync : HttpAsyncHandlerBase
    {
        /// <summary>
        /// 需要HttpAsyncHandlerBase类
        /// 需要Nito.AsyncEx、Nito.AsyncEx.Concurrent、Nito.AsyncEx.Enlightenment相关dll
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            //传递context 只是模拟一下传参
            string data = null;
            try
            {
                data = await DoSomething(0);
            }
            catch(Exception oe) { data = oe.Message+oe.StackTrace; }
            context.Response.Write(data);
        }

        public async Task<string> DoSomething(int x = 0)
        {

            //调用Delay 只是为了找个地方await  正常情况这里应该是await一个io行为
            await Task.Delay(0);

            return (1 / x).ToString();
        }
    }
}